import org.w3c.dom.ls.LSException;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 徐海涛
 * Date: 2021-11-29
 * Time: 10:08
 */
class  ListNode { //listNOde代表一个节点
    public int val;//数据域
    public ListNode next;//下一个节点的地址
    public  ListNode(int val) {//提供构造方法
        this.val = val;
    }
    //构造函数参数是val
}
public class MyLinkedList {//链表！！！
    public ListNode head;//链表的头引用；

    public void createList() { //创造个方法 //5个节点
        ListNode listNode1 = new ListNode(12);//引用和引用对象
        ListNode listNode2 = new ListNode(23);//引用产生一个新的对象
        ListNode listNode3 = new ListNode(34);//这些都是创造的节点
        ListNode listNode4 = new ListNode(45);//给这些节点赋值
        ListNode listNode5 = new ListNode(56);
        listNode1.next = listNode2;//穷举法，很low，暂时使用；
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;
    }

    public void display() {//遍历链表，链表打印的方式。
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;//遍历，想右遍历。
        }
        return false;
    }

    //得到单链表的长度
    public int size() {
        int count = 0;
        ListNode cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);//new个节点
        node.next = this.head;//不用判断
        this.head = node;
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);//创造个节点
        if (head == null) {//要判断head是不是null；
            head.next = node;
        } else {
            ListNode cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            //cur.next == null;
            cur.next = node;
        }
    }

    /**
     * 找到index-1位置节点的地址
     *
     * @param index
     * @return
     */
    public ListNode findIndex(int index) {
        ListNode cur = this.head;
        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        if (index < 0 || index > size()) {
            System.out.println("index位置不合法");
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = findIndex(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

    //找关键字前驱
    public ListNode searchPerv(int key) {
        ListNode cur = this.head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (this.head == null) {
            System.out.println("但链表为空不能删除");
            return;
        }
        if (this.head.val == key) {
            this.head = this.head.next;
        }

    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {

    }

    public void clear() {
    }

    //确定重复的元素1.重复的元素不止一个2.重复的元素，一定是紧挨在一起
    public ListNode deleteDuplication() {
        ListNode cur = head;
        ListNode newHead = new ListNode(-1);//虚拟节点
        ListNode tmp = newHead;
        while (cur != null) {
            //这里不仅要判断cur！=null，还要判断cur。next为为空，要考虑如cur下一个节点是不是空的
            if (cur.next != null && cur.val == cur.next.val) {//当前节点的值等于下一个几点的值
                while (cur.next != null && cur.val == cur.next.val) {
                    cur = cur.next;
                }
                cur = cur.next;
            } else {
                tmp.next = cur;
                tmp = tmp.next;
                cur = cur.next;
            }
        }
        return newHead.next;
    }

    public ListNode partition(int x) {//以给定值x为基准将链表分割成两部分，所有小于x的结点排在大于或等于x的结点之前。
        ListNode bs = null;//bs-be表示前一个段
        ListNode be = null;
        ListNode as = null;//as-ae表示后一个段
        ListNode ae = null;
        ListNode cur = null;
        while (cur != null) {
            if (cur.val < x) {//放在第一段
                //1.第一次把节点插入bs-be段此时bs-be都指向第一次插入的节点
                if (bs == null) {//be和bs都指向第一次的节点cur
                    be = cur;
                    bs = cur;
                } else {//如果不是第一次插入.尾插法
                    be.next = cur;//be.next等于第二次插入的cur的地址；
                    be = be.next;
                }
            } else {
                if (as == null) {
                    as = cur;
                    ae = cur;
                } else {
                    ae.next =cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if(bs==null) {
            return as;
        }
        be.next = as;//将bs-be段和as-ae段拼接起来
        if(as!=null) {//如果最后一个节点不为空我们需要手动将他置为空
            ae.next = null;//！！！！
        }
        return bs;

        //1.是所有的数据都是小于x吗？
        //2.最后一个数据一定是大于x的吗？
        //如果不是小于x那么我们必须要将最后一个节点手动置为null，所以这种方法不好！
        //be.next = as;
        //return bs;
    }
     public ListNode FindKthToTail(ListNode head,int k) {
        if(k<=0|| head == null) {
            return null;
        }
        ListNode fast = head;//定义两个头
        ListNode slow = head;
        while (k-1 != 0) {//先让fast走k-1布
            fast = fast.next;
            k--;//没走一步k--；
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
     }
    public ListNode middleNode(ListNode head) {
        if(head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    public ListNode reverseList(ListNode head) {
        if(this.head == null){
            return null;
        }
        ListNode cur =this.head.next;
        ListNode pre = null;
        while (cur!=null){
            ListNode curNext = cur.next;//在cur.next修改之前引入curNext来存储cur.next
            cur.next = pre;
            cur = curNext;
        }
        return pre;//pre指向的节点就是反转后的头节点
    }
    public ListNode reverseList1(ListNode head) {
        if(this.head == null) {
            return null;
        }
        ListNode cur = this.head;
        ListNode pre = cur.next;
        while (cur!=null) {
            ListNode curNext = cur.next;
            cur.next = pre;
            cur = cur.next;
        }
        return pre;
    }
    public ListNode middleNode1(ListNode head) {
        if(this.head == null) {
            return null;
        }
        ListNode fast = null;
        ListNode slow = null;
        ListNode cur = head;
        while (fast != null && fast.next !=null) {
           fast = fast.next.next;
           slow = slow.next;
        }
        return slow;
    }
    public  ListNode FindkthTOtail1(ListNode head,int k) {
        ListNode fast = head;
        ListNode slow = head;
        ListNode cur = head;
        if(k<0 || head == null) {
            return null;
        }
        while (k-1 !=0) {
            fast = fast.next;
            k--;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    public  ListNode partition1(int x) {
        ListNode be = null;
        ListNode bs = null;
        ListNode ae = null;
        ListNode as = null;
        ListNode cur = null;
        while (cur != null) {
            if (cur.val<x) {
                if(bs == null) {
                    be = cur;
                    bs = cur;
                } else {
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if(as == null) {
                    as = cur;
                    ae = cur;
                } else {
                    ae.next =cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if(bs == null) {
            return as;
         }
        be.next = as;
        if(as!=null) {
            ae.next =null;
        }
        return bs;
    }


//链表回文
    public boolean chkPalindrome(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast!=null && fast.next!=null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode cur = slow.next;
        ListNode curNext = cur.next;
        while (cur != null) {
            cur = slow.next;
            slow = cur;
            cur = cur.next;
        }
        while (head != slow) {
            if(head.val != slow.val) {
                return false;
            }
            if(head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }
    public  boolean hascycle(ListNode head) { //判断是否有换脸
        ListNode fast = head;
        ListNode slow = head;
        while (fast!=null && fast.next!=null) {
            fast = fast.next.next;
            slow = slow.next.next;
        }
        if(fast == slow) {
            return true;
        }
        return false;
    }

    public void creatLoop() {//构造一个环链
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = head.next.next;
    }



























}
